/**
 * 
 */
package org.sunnyleaf.votedemo.client.ui;

import java.util.ArrayList;
import java.util.Date;

import org.sunnyleaf.votedemo.client.GreetingServiceAsyncFactory;
import org.sunnyleaf.votedemo.client.Votedemo;
import org.sunnyleaf.votedemo.client.data.NewVoteRequest;
import org.sunnyleaf.votedemo.client.data.Vote;
import org.sunnyleaf.votedemo.client.data.VoteItem;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

/**
 * @author Administrator
 * 
 */

public class VoteEditPanel extends Composite {

	private static int vinstanceId = 0;

	private VerticalPanel questionList = new VerticalPanel();
	private Vote vote;
	private TextBox title = new TextBox();
	private Button comfirm = new Button("完成");
	private Button cancel = new Button("取消");

	private TextArea mailList = new TextArea();
	private TextArea msg = new TextArea();

	private Votedemo votedemo;

	private void reset(){
		title.setValue("");	
		questionList.clear();
		questionList.add(new VoteItemOption());			
	}
	
	public VoteEditPanel() {

		HorizontalPanel mainPanel = new HorizontalPanel();
		VerticalPanel vp = new VerticalPanel();
		vp.add(new HTML("标题"));
		vp.add(title);
		vp.add(questionList);

		questionList.add(new VoteItemOption());

		HorizontalPanel hp = new HorizontalPanel();

		Button newQ = new Button("添加问题");
		newQ.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				questionList.add(new VoteItemOption());
			}
		});
		hp.add(newQ);
		hp.add(comfirm);
		hp.add(cancel);
		vp.add(hp);

		VerticalPanel vp2 = new VerticalPanel();
		vp2.add(new HTML("邀请人列表（每行一个）"));
		mailList.setHeight("100px");
		vp2.add(mailList);
		vp2.add(new HTML("附加消息"));
		msg.setHeight("100px");
		vp2.add(msg);

		this.comfirm.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// 调用RPC
				if( mailList.getText().isEmpty() ){
					Window.alert("通知人列表不能为空");
					return;
				}

				NewVoteRequest nvr = new NewVoteRequest();
				ArrayList<VoteItem> vis = new ArrayList<VoteItem>();
				Vote v = new Vote();
				v.setOwner( Votedemo.getEmail());
				v.setStartDate(new Date());
				v.setStatus(Vote.OPEN);
				v.setVoteName(title.getText());

				for (int i = 0; i < questionList.getWidgetCount(); i++) {
					Widget w = questionList.getWidget(i);
					if(w instanceof VoteItemOption){
						VoteItemOption vio = (VoteItemOption) w;
						VoteItem vi = new VoteItem();
						vi.setQuestion(vio.question.getText());
						if(vio.sb.getValue()){
							vi.setType(VoteItem.SINGLE);
						}else{
							vi.setType(VoteItem.MULTI);
						}

						vi.setAbstainable(vio.cb.getValue());
						
						for(int j = 0; j<vio.optionPanel.getWidgetCount();j++){
							FlowPanel fp1 = (FlowPanel) vio.optionPanel.getWidget(j);
							TextBox tb = (TextBox) fp1.getWidget(1);
							if(tb.getText().length() != 0){
								vi.addItem(tb.getText());
							}
						}
						vi.setVoteID(v.getId());
						vis.add(vi);
					}
				}
				v.setVoteItems(vis);
				nvr.setVote(v);
				nvr.setMsg(msg.getText());
				nvr.setRecipients(mailList.getText());
				vote = nvr.getVote();

				GreetingServiceAsyncFactory.getServiceInstance().addNewVote(nvr, new AsyncCallback<Vote>() {
					public void onFailure(Throwable caught) {
						Window.alert("Error");
					}

					public void onSuccess( Vote result ) {
						vote = result; 
						votedemo.updateSponseredVotePanel();
						if( vote.getIsParticipated() ){
							votedemo.updateAttendVotePanel( vote, Votedemo.UPDATE_TYPE.ADD, false );
						}
						votedemo.activePanel(1);
						Window.alert("投票添加成功");
						reset();
					}
				});
			}
		});

		mainPanel.add(vp);
		mainPanel.add(vp2);

		this.initWidget(mainPanel);
	}

	class VoteItemOption extends Composite {

		private TextBox question = new TextBox();
		private RadioButton sb ;
		private RadioButton mb ;
		private CheckBox cb;
		
		private VerticalPanel optionPanel = new VerticalPanel();

		private Button closeButton;

		private int instanceId;

		private ArrayList<TextBox> textBoxes = new ArrayList<TextBox>();

		public VoteItemOption() {
			this.instanceId = vinstanceId++;
			FlexTable ft = new FlexTable();
			FlexCellFormatter cf = ft.getFlexCellFormatter();

			ft.setWidth("400px");
			ft.setCellSpacing(3);
			ft.setCellPadding(3);

			HorizontalPanel hp0 = new HorizontalPanel();
			hp0.add(new HTML("问题" + ": "));
			hp0.add(question);
			ft.setWidget(0, 0, hp0);
			cf.setColSpan(0, 0, 3);

			HorizontalPanel hp1 = new HorizontalPanel();
			hp1.setSpacing(10);
			sb = new RadioButton("selection" + vinstanceId, "单选");
			sb.setValue(true);
			mb = new RadioButton("selection" + vinstanceId, "多选");

			cb = new CheckBox("可弃权");
			cb.setValue(true);

			hp1.add(sb);
			hp1.add(mb);
			hp1.add(cb);

			ft.setWidget(1, 0, hp1);

			ft.setWidget(1, 1, new HTML(""));

			closeButton = new Button("x");
			closeButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					questionList.remove(VoteItemOption.this);
				}
			});

			ft.setWidget(1, 2, closeButton);
			cf.setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_RIGHT);

			optionPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			ft.setWidget(2, 0, optionPanel);
			cf.setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
			cf.setColSpan(2, 0, 3);

			Hyperlink newLine = new Hyperlink("添加选项", "#");
			newLine.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					appendEditLine();
				}
			});

			ft.setWidget(3, 0, newLine);

			this.appendEditLine();

			ft.setStyleName("vl-flextable");

			initWidget(ft);
		}

		private void appendEditLine() {
			FlowPanel fp = new FlowPanel();
			RadioButton rb = new RadioButton("vlio-" + vinstanceId, "");
			TextBox tb = new TextBox();
			fp.add(rb);
			fp.add(tb);
			this.textBoxes.add(tb);
			this.optionPanel.add(fp);
		}
	}

	public void setMainPanel(Votedemo vd) {
		this.votedemo = vd;
	}
}
